/*
 * @Author: duanguang
 * @Date: 2021-11-24 23:20:35
 * @LastEditTime: 2022-03-03 16:05:48
 * @LastEditors: duanguang
 * @Description: 
 * @FilePath: /legions-server/app/service/repository_project.ts
 * 「扫去窗上的尘埃，才可以看到窗外的美景。」
 */
/*
 * @Author: linzeqin
 * @Date: 2019-09-30 09:07:07
 * @description: 沙箱资源调度service
 */
import { Service } from 'egg';
import { IRepositoryProjectModel, RepositoryProject } from '@/model/repository_project';
import { PageModel } from '@/model/responseModel';
import { UserRoleEnum } from '@/constants/enum.types';

export default class RepositoryProjectService extends Service {
    

    /**
     * 新增
     * @memberof RepositoryProjectService
     */
    public async createProject(params: Partial<RepositoryProject>) {
        /** 项目名称唯一性校验 */
        if (await this.ctx.model.RepositoryProject.findOne({
            project_name: params.project_name,
        })) {
            throw '项目名称已存在';
        }
        /** 新增 */
        try {
            return await this.ctx.model.RepositoryProject.create({
                ...params,
            } as RepositoryProject);
        } catch (error) {
            this.logger.error(error);
            throw error;
        }
    }
    public async updateProject(params: Partial<RepositoryProject> & {
        id: string,
        userRule: Number,
        userId:string
    }) {
        if (params.id) {
            try {
                if (await this.ctx.model.RepositoryProject.findOne({
                    project_name: params.project_name,
                    _id: { $ne: params.id },
                })) {
                    throw `名称为【${params.project_name}】的项目已存在`;
                }
                const {userId,userRule,id,...props} =params
                const res = await this.ctx.model.RepositoryProject.updateOne({
                    _id: params.id,
                    ...userRule > UserRoleEnum.admin&&{creator_id:userId}
                },{
                    ...props,
                });
                if (res.nModified) {
                    return await this.ctx.model.RepositoryProject.findById(params.id);
                }
            } catch (error) {
                
            }
        }
    }
    public async getList(params: {
        /** 当前页 */
        pageIndex: string,
        /** 每页条数 */
        pageSize: string,
        /** 名称 */
        project_name: string,
        userRule: Number,
        userId:string
    }) {
        try {
            const project_name = params.project_name || ''
            const pageIndex = parseInt(params.pageIndex,10)
            const pageSize = parseInt(params.pageSize,10)
            console.log(params.userRule)
            const condition = {
                $match: {
                    ...project_name.trim() && { project_name: { $regex: project_name } },
                    // ...{ roleNo: {$gt:UserRoleEnum.admin}}
                }
            }
            const list = await this.ctx.model.RepositoryProject.aggregate([
                {
                    ...condition
                },
                {
                    $sort: {
                        update_time: -1,
                    },
                },
                {
                    $skip: (pageIndex - 1) * pageSize,
                },
                {
                    $limit: pageSize,
                },
            ])
            const total: number = await this.ctx.model.RepositoryProject.find(condition.$match).count();
            return new PageModel<IRepositoryProjectModel>({
                list,
                pageIndex: pageIndex || 1,
                pageSize: pageSize || 10,
                total,
            });
        } catch (error) {
            this.logger.error(error);
            throw error;
        }
    }
}
